################################################################################ 
#
# The distribution of hate speech and its implications for content moderation
# PSRM - Replication package
# Master file
#
################################################################################# 

rm(list = ls())

# Optional: load renv environment (usually handled by .Rprofile)
if (!requireNamespace("renv", quietly = TRUE)) install.packages("renv")
renv::activate()

# START LOGGING
wd_log_file <- file("run_log.txt", open = "wt")
sink(wd_log_file)
sink(wd_log_file, type = "message")

# Record start time
start_time <- Sys.time()
cat("### SCRIPT STARTED AT:", format(start_time, "%Y-%m-%d %H:%M:%S"), "\n\n")

library(dplyr)
library(tidyr)
library(readr)
library(ggplot2)
library(fastDummies)
library(lubridate)
library(forcats)
library(patchwork)
library(kableExtra)
library(scales)
library(estimatr)
library(glmnet)
library(hdm)
library(readxl)
library(writexl)
library(xtable)
library(broom)
library(gtools)
library(retrodesign)
library(cowplot)

################################################################################ 
#   DATA AND FOLDER
################################################################################ 

# wd = Sys.getenv("WD_PSRM"); setwd(wd) # if you're not working with the R Project, set your wd manually
wd = getwd()
wd_res = file.path(wd, 'results')
wd_data = file.path(wd, 'data')
wd_data_processed = file.path(wd, 'data_processed')

################################################################################ 
#   CREATE DIRECTORIES IF NECESSARY
################################################################################ 

# define the directories to create
dirs_to_create <- c(
  wd_res,
  file.path(wd_data_processed, "for_analysis"),
  file.path(wd_data_processed, "twitter_user_info_aggregated"),
  file.path(wd_res, "tables"),
  file.path(wd_res, "figures")
)

# create directories if they don't exist
for (dir_path in unique(dirs_to_create)) {
  if (!dir.exists(dir_path)) {
    dir.create(dir_path, recursive = TRUE)
    message(paste0("Created directory: ", dir_path))
  }
}

################################################################################ 
#   SET RUN PARAMETERS
################################################################################ 

setsave = T

################################################################################ 
#   RUN SCRIPTS
################################################################################ 

# setup
source("code/01_data_creation.R")

# twitter descriptives
source("code/03_tabC1.R")
source("code/04_tabC2.R")
source("code/05_figC5.R")

# classifiers
source("code/06_tabD3.R")
source("code/07_tabD4.R")
source("code/08_figD6.R")

# experiment
source("code/09_tabE5_main_analysis.R")
source("code/10_tabE6.R")
source("code/11_tabE7.R")
source("code/12_tabE8.R")
source("code/13_tabE9.R")
source("code/14_tabE10.R")
source("code/15_tabE11.R")
source("code/16_tabE12.R")
source("code/17_tabE13.R")
source("code/18_tabE14.R")
source("code/19_tabE15.R")
source("code/20_tabF16.R")
source("code/21_figG8_tabG18-G21.R")
source("code/22_tabG17.R")

# main paper figures
source("code/02_fig1_figC3_figC4.R")
source("code/23_fig2.R")

# END LOGGING
end_time <- Sys.time()
cat("\n### SCRIPT ENDED AT:", format(end_time, "%Y-%m-%d %H:%M:%S"), "\n")

sink(type = "message")
sink()
close(wd_log_file)

